package jt.classic.system.action;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.utils.AnnexTool;
import jt.classic.system.utils.BaseworkSelectUtil;
import jt.classic.system.utils.ResultJsonLoader;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.limp.basework.Column;
import org.limp.basework.Table;
import org.limp.basework.analyzer.TableRegisterCenter;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.mine.DataTrimmerI;
import org.limp.mine.StringTool;
import org.limp.mine.DataTrimmerI.DataItemList;
import org.limp.mine.annex.AnnexInfo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 数据查询控制器
 * <p>
 * select primary table and foreign table param is json, for example "{ id :
 * "01", primaryTable : "primaryTest", foreignTables : [ {foreignTable :
 * "foreignTest1", orderby : "columnTest1", sort : "ASC" },{ foreignTable :
 * "foreignTest2", orderby : "columnTest2", sort : "DESC" }] }"
 * </p>
 * 
 * @author mido
 * 
 */
public class BaseWorkSelectIIAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Connection conn = null;

		CommonBean4HSQ cb = new CommonBean4HSQ(request);
		Map param = cb.getResource();

		String jsonparams = StringTool.checkString(param.get("jsonparams"));

		JSONObject jsonobj = JSONObject.parseObject(jsonparams);

		if (jsonobj == null) {
			ResultJsonLoader.loadErrorJson(request, "jsonparams is null.");
		} else {
			String id = StringTool.checkString(jsonobj.get("id"));
			String primaryTableName = StringTool.checkString(jsonobj
					.get("primaryTable"));

			TableRegisterCenter center = TableRegisterCenter.getInstance();

			if (center.containsTable(primaryTableName)) {
				Table primaryTable = center.findTable(primaryTableName);

				try {
					SystemDataSource ds = SystemDataSource.getInstance();
					conn = ds.getConnection();

					BaseworkSelectUtil selectUtil = new BaseworkSelectUtil(conn);

					Map resultData = selectUtil.selectSingleData(primaryTable,
							id);

					if (resultData != null) {
						if (jsonobj.containsKey("foreignTables")) {
							Column keyColumn = primaryTable.column(primaryTable
									.getPrimaryKey()[0]);

							JSONArray foreignTables = (JSONArray) jsonobj
									.get("foreignTables");
							for (int i = 0; i < foreignTables.size(); i++) {
								JSONObject itemObject = (JSONObject) foreignTables
										.get(i);

								String foreignTableName = StringTool
										.checkString(itemObject
												.get("foreignTable"));
								String listid = StringTool.defaultForEmpty(
										StringTool.checkString(itemObject
												.get("listid")),
										foreignTableName);

								String orderby = (String) itemObject
										.get("orderby");
								String sort = (String) itemObject.get("sort");

								Table foreignTable = center
										.findTable(foreignTableName);

								Column foreignColumn = foreignTable
										.foreignKeyColumn4PrimaryTableColumn(keyColumn);

								List foreignData = selectUtil.selectMultiData(
										foreignTable, foreignColumn.getName(),
										id, orderby, sort);

								resultData.put(listid, foreignData);
							}
						}

						ResultJsonLoader.loadSuccessJson(request, resultData);
					} else {
						ResultJsonLoader.loadSuccessJson(request, null);
					}
				} catch (Exception ex) {
					ResultJsonLoader.loadErrorJson(request, ex.getMessage());
					ISystem.catchException(request, this, ex);
				} finally {
					SystemDataSource.closeConnection(conn);
				}
			} else {
				ResultJsonLoader.loadErrorJson(request,
						"primary table not exists.");
			}
		}
		return mapping.findForward("jsonresult");
	}
}
